package com.example.weblending.controlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.ApplyCondition;
import com.example.weblending.domain.Post;
import com.example.weblending.domain.User;
import com.example.weblending.service.ApplyConditionService;
import com.example.weblending.service.PostService;
import com.example.weblending.service.UserService;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.Result;
import com.example.weblending.vo.CsvInputColumn;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/post")
@CrossOrigin
public class PostController {
    @Resource
    private UserService userService;
    @Resource(name = "postServiceImpl")
    private PostService postService;

    /**
     * 获得已经发布的公告列表
     */
    @GetMapping("{page}/{limit}")
    public Result show(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        Page<Post> conditionPage =postService.getShow(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 获得某一帖子的详细信息
     */
    @GetMapping("{id}")
    public Result show(@PathVariable String id, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        Post post=postService.getById(id);
        return Result.ok().data("item",post);
    }

    /**
     * 获得未发布的公告列表
     */
    @GetMapping("examine/{page}/{limit}")
    public Result examine(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<Post> conditionPage =postService.getExamine(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 获得已发布的公告列表
     */
    @GetMapping("approve/{page}/{limit}")
    public Result approve(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<Post> conditionPage =postService.getApprove(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 新增一条公告
     */
    @PostMapping("")
    public Result create(@RequestBody Post post, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success= postService.create(post,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 发布指定公告
     */
    //增加清洗次数
    @PostMapping("/pass/{id}")
    public Result pass(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=postService.setState(id,1,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 下架指定公告
     */
    @PostMapping("deny/{id}")
    public Result deny(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=postService.setState(id,0,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 将公告设置为重要公告，如果该公告还没有发布则发布
     */
    @PostMapping("prime/{id}")
    public Result refresh(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success=postService.setState(id,2,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 删除公告
     */
    @DeleteMapping("")
    public Result delete(@RequestBody int[] list, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success = postService.delete(list,user);
        return success?Result.ok():Result.error();
    }
}
